#include "AfterBootloader.h"

/// <summary>
/// 发送起始标识
/// </summary>
uint8_t g_AfterBootloader_Receive_StartFlag[2] = { 0xFA, 0x55 };
/// <summary>
/// 发送结束标识
/// </summary>
uint8_t g_AfterBootloader_Receive_EndFlag[2] = { 0x56, 0xFB };
        
/// <summary>
/// Bootloader帧
/// </summary>
uint8_t m_AfterBootloader_EntryBootloaderCmd[12] = { 0xFA, 0x55,  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x56, 0xFB };

/// <summary>
/// 复位帧
/// </summary>
uint8_t m_AfterBootloader_ResetCmd_g[12] = { 0xFA, 0x55, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x56, 0xFB };

/// <summary>
/// 检查Bootloader帧
/// </summary>
uint8_t m_AfterBootloader_CheckBootloaderCmd[12] = { 0xFA, 0x55, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x56, 0xFB };

/// <summary>
/// 发送起始标识
/// </summary>
uint8_t m_AfterBootloader_Send_StartFlag[2] = { 0xAF, 0xAA };

/// <summary>
/// 发送结束标识
/// </summary>
uint8_t m_AfterBootloader_Send_EndFlag[2] = { 0xA5, 0xAF };

/// <summary>
/// 接收到进入Bootloader帧，反馈
/// </summary>
uint8_t g_AfterBootloader_EntryBootloaderReponse[12] = { 0xAF, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA5, 0xAF };

/// <summary>
/// 接收到进入Reset帧，反馈
/// </summary>
uint8_t g_AfterBootloader_ResetReponse[12] = { 0xAF, 0xAA, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA5, 0xAF };

//比对数组，若完全一致，返回0，否则返回1
uint8_t ArrayCheck(uint8_t *data1,uint8_t *data2,uint8_t length)
{
    uint8_t i,result=0;
    for(i=0;i<length;i++)
    {
        if(data1[i]!=data2[2])
        {
            result=1;
            return result;
        }
    }
    return result;
}

//数据解析，返回0xff未异常
uint8_t AfterBootloader_DataParse(uint8_t *data, uint8_t dataLength)
{
    uint8_t result=0;
    if(dataLength!=12)
        return 0xff;
    
    //命令类型
    switch(data[2])
    {
        case EntryBootloader:
            result = EntryBootloader;
            break;
        case Reset:
            result = Reset;
            break;
        case Data:
            result = Data;
            
            break;
        case CheckBootloader:
            result = CheckBootloader;
            break;
        default:return 0xff;
    }
    
    return result;
}